<?php
namespace app\api\service;
use think\Controller;
use Des3\DES3;
use think\Db;

class MemberRoleInfo extends Controller {
    static protected $err_code = 0;

    // 获取基本信息资料
    static public function get_member_role_info($param) {
        if(!isset($param['member_id']) ||
            !isset($param['type'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        $where = [
            'member_id' => $param['member_id'],
            'type' => $param['type']
        ];

        try{
            $member_info = db('member_role_info')->where($where)->find();

            // 获取实名认证信息
            $models = ['member', 'repair', 'dealer', 'partner'];
            $model = $models[$param['type']];
            if ($model) {
                if ($param['type'] == 0) {
                    $map['id'] = $param['member_id'];
                } else {
                    $map['member_id'] = $param['member_id'];
                }
                $is_certification = db($model)->where($map)->value('is_certification');
                $member_info['is_certification'] = $is_certification;
            }

            // 获取我的默认爱车
            $my_car = db('my_car')->where('member_id', $param['member_id'])->order('is_default desc')->find();
            $member_info['my_car'] = $my_car;

            return ['code' => 200, 'data' => $member_info, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }

    }

    // 获取基本资料字段信息
    static public function get_field_info($param) {
        if(!isset($param['member_id']) || !isset($param['field']) ||
            !isset($param['type'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $map['member_id'] = $param['member_id'];
            $map['type'] = $param['type'];
            $value = db('member_role_info')->where($map)->value($param['field']);
            return ['code' => 200, 'data' => ['value' => $value], 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 修改基本信息
    static public function save_role_info($param) {
        if(!isset($param['member_id']) || !isset($param['field']) || !isset($param['value']) ||
            !isset($param['type'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $map['member_id'] = $param['member_id'];
            $map['type'] = $param['type'];
            if ($param['field'] == 'birthday') {
                $param['value'] = strtotime($param['value']);
            }
            if ($param['field'] == 'phone') {
                /*调用验证器 验证数据*/
                $validate = validate('Member');
                if(!$validate->scene('phone')->check(['phone' => $param['value']])){
                    self::$err_code = 400026;
                    throw new \Exception('FAULT');
                }
            }
            $data[$param['field']] = $param['value'];
            $member_role_info = db('member_role_info')->where($map)->find();
            if (!$member_role_info) {
                $map[$param['field']] = $param['value'];
                db('member_role_info')->insert($map);
            } else {
                db('member_role_info')->where($map)->update($data);
            }
            return ['code' => 200, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 实名认证信息
    static public function certification_info($param) {
        if(!isset($param['member_id']) || !isset($param['type'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        try{
            $models = ['member', 'repair', 'dealer', 'partner'];
            $model = $models[$param['type']];
            if ($param['type'] == 0) {
                $where['id'] = $param['member_id'];
            } else {
                $where['member_id'] = $param['member_id'];
            }
            $certification_info = db($model)->field('proposer, card_no, card_no as card_no_all, is_certification,certification_msg,id_card_in_hand,id_card_front,id_card_verso')
                ->where($where)
                ->find();
            if ($certification_info['card_no']) {
                $len = strlen($certification_info['card_no']) - 8;
                $certification_info['card_no'] = substr($certification_info['card_no'], 0, 4) . str_repeat('*', $len) . substr($certification_info['card_no'], -4);
            }
            return ['code' => 200, 'data' => ['certification_info' => $certification_info], 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 修改实名认证信息
    static public function save_certification($param) {
        if(!isset($param['member_id']) || !isset($param['type'])
            || !isset($param['proposer']) || !isset($param['card_no'])
            || !isset($param['id_card_in_hand'])
            || !isset($param['id_card_front']) || !isset($param['id_card_verso'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $models = ['member', 'repair', 'dealer', 'partner'];
            $model = $models[$param['type']];

            $data = [
                'proposer' => $param['proposer'],
                'card_no' => $param['card_no'],
                'id_card_in_hand' => $param['id_card_in_hand'],
                'id_card_front' => $param['id_card_front'],
                'id_card_verso' => $param['id_card_verso'],
                'is_certification' => 1 // 实名认证状态改为已提交
            ];

            // 身份证实名认证
            $host = "http://idcard.market.alicloudapi.com";
            $path = "/lianzhuo/idcard";
            $method = "GET";
            $appcode = "23b6b7fe2b9840168b2ce4bfe06c2075";
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);
            $querys = "cardno=". $param['card_no'] ."&name=" . $param['proposer'];
            $bodys = "";
            $url = $host . $path . "?" . $querys;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, true);
            if (1 == strpos("$".$host, "https://"))
            {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            $rs =  curl_exec($curl);
            $rs = substr($rs, strpos($rs, '{'));
            $rs = json_decode($rs, true);
            if ($rs['resp']["code"] > 0 ) {
                switch ($rs['resp']["code"]) {
                    case 5:
                        self::$err_code = 400143;
                        throw new \Exception('FAULT');
                        break;
                    case 14:
                        self::$err_code = 400144;
                        throw new \Exception('FAULT');
                        break;
                }
            }

            /*调用验证器 验证数据*/
            $validate = validate('Certification');
            //判断是否可以申请

            if (!$validate->check($param)) {
                return ['code'=>$validate->getError(),'msg'=> '提交资料不完整，请确认'];
            }

            if ($param['type'] == 0) {
                $where['id'] = $param['member_id'];
                $check_where['id'] = ['neq', $param['member_id']];
            } else {
                $where['member_id'] = $param['member_id'];
                $check_where['member_id'] = ['neq', $param['member_id']];
                $data['audit_status'] = 1;// 角色审核状态改为待审核
            }

            $check_where['card_no'] = $param['card_no'];
            $rs = db($model)->where($check_where)->find();

            if ($rs) {
                self::$err_code = 400064;//别人用了这个身份证
                throw new \Exception('FAULT');
            }else{
                db($model)->where($where)->update($data);
            }
            return ['code' => 200, 'data' => '', 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }
}
